package cn.yexiaofeng.bookmanager.service;


import cn.yexiaofeng.bookmanager.beans.BookInfo;
import cn.yexiaofeng.bookmanager.beans.LendList;
import cn.yexiaofeng.bookmanager.dao.BookInfoMapper;
import cn.yexiaofeng.bookmanager.dao.LendListMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 图书
 */


@Service
public class BookServiceImpl implements BookInfoService {

     @Autowired
     BookInfoMapper bookInfoMapper;

     @Autowired
     private LendListMapper lendListMapper;


    @Override
    public List<BookInfo> getAllBooks() {
        return bookInfoMapper.getAllBooks();
    }
    @Override
    public List<BookInfo> getBooksByLike(Map map) {
        String bookName = map.get("bookName").toString();
        if (bookName.trim() != null && bookName.trim() != "") {
            List<BookInfo> booksByLike = bookInfoMapper.getBooksByLike(map);
            System.out.println(booksByLike);
            return bookInfoMapper.getBooksByLike(map);
        }
        return null;
    }
    @Override
    public List<BookInfo> getBookByAuthor(Map map) {
        String author = map.get("author").toString();
        if (author.trim() != null && author.trim() != "") {
            return bookInfoMapper.getBookByAuthor(map);
        }
        return null;
    }
    @Override
    public BookInfo getBooksByNameAndAuthor(Map<String, String> map) {
        if (StringUtil.isNotEmpty(map.get("bookName")) && StringUtil.isNotEmpty(map.get("author"))) {
            check(map);
            List<BookInfo> booksByNameAndAuthor = bookInfoMapper.getBooksByNameAndAuthor(map);
            System.out.println(booksByNameAndAuthor);
            return bookInfoMapper.getBooksByNameAndAuthor(map).get(0);
        } else if (StringUtil.isNotEmpty(map.get("bookName")) && !StringUtil.isNotEmpty(map.get("author"))) {
            return getBooksByLike(map).get(0);
        } else if (StringUtil.isNotEmpty(map.get("author")) && !StringUtil.isNotEmpty(map.get("bookName"))) {
            return getBookByAuthor(map).get(0);
        } else {
            return null;
        }
    }


    /**
     * 检查是有表关联
     */
    public  void check(Map map){
        BookInfo bookInfo = bookInfoMapper.getBooksByNameAndAuthor2(map);
        String bookId=null;
        if (bookInfo!=null){
            bookId=bookInfo.getBook_id();
            LendList readerIdByBookId = lendListMapper.getReaderIdByBookId(bookId);
            map.put("book_id",bookId);
            if (readerIdByBookId!=null ){
                map.put("reader_id",readerIdByBookId.getReader_id());
            }else {
                map.put("reader_id",null);
            }
        }

    }

    @Override
    public List<BookInfo> getBooksByPage(String currPage, String pageSize) {
        PageHelper.startPage(Integer.parseInt(currPage),Integer.parseInt(pageSize));
        List<BookInfo> allBooks = bookInfoMapper.getAllBooks();
        return allBooks;
    }

    @Override
    public Integer addInfo(BookInfo bookInfo) {
        return bookInfoMapper.addInfo(bookInfo);
    }

    @Override
    public Integer deleBook(Integer bookId) {
        return bookInfoMapper.deleBook(bookId);
    }
}